package com.meme.editor.property.value;

import org.apache.log4j.Logger;

import com.meme.editor.property.Property;
import com.meme.editor.property.PropertyValue;
import com.meme.editor.property.PropertyControl;
import com.meme.editor.property.control.InputPropertyControl;
import com.meme.editor.property.PropertyFactory;


public class NumberPropertyValue extends PropertyValue
{
	static Logger logger = Logger.getLogger (NumberPropertyValue.class);
	
	float mTheValue;
	
	public NumberPropertyValue (Property property)
	{
		super (property);
	}

	public int intValue ()
	{
		return (int)mTheValue;
	}

	public float floatValue ()
	{
		return mTheValue;
	}
	
	@Override
	public Object getValue ()
	{
		return new Float (mTheValue);
	}
	
	@Override
	public String toString ()
	{
		return "" + mTheValue;
	}
	
	@Override
	public String getDefaultControlType ()
	{
		//return PropertyFactory.ControlType_Number;
		return PropertyFactory.ControlType_Input;
	}

	@Override
	protected boolean modify (Object newValue)
	{
		if ( newValue instanceof NumberPropertyValue )
		{
			NumberPropertyValue value = (NumberPropertyValue)newValue;
			mTheValue = value.mTheValue;
			
			return true;
		}
		else if ( newValue instanceof Float )
		{
			Float f = (Float)newValue;
			mTheValue = f.floatValue();
			
			return true;
		}
		else if ( newValue instanceof Integer )
		{
			Integer i = (Integer)newValue;
			mTheValue = i.floatValue();
			
			return true;
		}
		else if ( newValue instanceof String )
		{
			try
			{
				String s = (String)newValue;
				if (s.startsWith("0x") || s.startsWith("0X"))
					mTheValue = Integer.parseInt(s, 16);
				else
					mTheValue = Float.parseFloat(s);
				
				return true;
			}
			catch (Exception e)
			{

			}
		}

		logger.error ("newValue may be not valid number value.");
		
		return false;
	}
	
	@Override
	public String getValueAsString ()
	{
		return "" + mTheValue;
	}
	
	@Override
	protected Object   makeLegalValueFromControl (Object[] params)
	{
		float newValue = mTheValue;
		
		try
		{
			newValue = Float.parseFloat( (String) params[0] );
		}
		catch (Exception e)
		{
			logger.error ("The value specified can't be converted into an legal number.");
		}
		
		return new Float (newValue);
	}
	
	@Override
	protected Object[] makeDataForControl ()
	{
		return new Object[] {toString()};
	}

	@Override
	protected boolean _setParameter (String name, Object value)
	{
		return false;
	}
}
